package com.breeze.leetcode.middle;

import java.util.HashMap;
import java.util.Map;

/**
 * @url <a href="https://leetcode.cn/problems/range-frequency-queries">2080. 区间内查询数字的频率</a>
 * @author breeze
 * @date 2025/2/18
 */
public class T2080RangeFreqQuery {
    private final int[] arr;
    private Map<String, Integer> resultMap = new HashMap<>();

    public T2080RangeFreqQuery(int[] arr) {
        this.arr = arr;
    }

    public int query(int left, int right, int value) {
        String key = left + "-" + right + "-" + value;
        if (resultMap.containsKey(key)) {
            return resultMap.get(key);
        }

        int result = 0;
        for (int i = left; i <= right; i++) {
            if (arr[i] == value) {
                result++;
            }
        }

        resultMap.put(key, result);
        return result;
    }
}
